package com.ryx.service.Impl;

import com.alibaba.fastjson.JSON;
import com.ryx.entity.SysUser;
import com.ryx.mapper.SysUserMapper;
import com.ryx.service.HomeService;
import com.ryx.service.PermissionService;
import com.ryx.vo.resp.HomeRespVo;
import com.ryx.vo.resp.PermissionRespNodeVo;
import com.ryx.vo.resp.UserInfoRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsPackage: com.ryx.service.Impl
 * @Author: 容永轩
 * @CreateTime: 2020-12-23
 * @Description:
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private PermissionService permissionService;

    @Override
    public HomeRespVo getHome(String userId) {

        HomeRespVo homeRespVo = new HomeRespVo();
        /**
         * mock 导航菜单数据后期直接从DB获取
         */
//        String home="[\n" +
//                "    {\n" +
//                "        \"children\": [\n" +
//                "            {\n" +
//                "                \"children\": [],\n" +
//                "                \"id\": \"3\",\n" +
//                "                \"title\": \"菜单权限管理\",\n" +
//                "                \"url\": \"/index/menus\"\n" +
//                "            }\n" +
//                "        ],\n" +
//                "        \"id\": \"1\",\n" +
//                "        \"title\": \"组织管理\",\n" +
//                "        \"url\": \"string\"\n" +
//                "    },\n" +
//                "    {\n" +
//                "        \"children\": [],\n" +
//                "        \"id\": \"2\",\n" +
//                "        \"title\": \"类目2\",\n" +
//                "        \"url\": \"string\"\n" +
//                "    }\n" +
//                "]";
        //从字符串中提取data
//        List<PermissionRespNodeVo> list= JSON.parseArray(home,PermissionRespNodeVo.class);

        List<PermissionRespNodeVo> list = permissionService.permissionTreeList(userId);

        //每人人登录他的菜单栏首页是不一样的 ，所有传入了userId
        SysUser user = userMapper.selectByPrimaryKey(userId);

        UserInfoRespVo userInfoRespVo = new UserInfoRespVo();
        if (null != user){
            BeanUtils.copyProperties(user,userInfoRespVo);
            userInfoRespVo.setDeptName("轩哥最帅");

        }

        //把数据注入
        homeRespVo.setMenus(list);
        homeRespVo.setUserInfoVO(userInfoRespVo);

        return homeRespVo;
    }
}
